/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.util.List; 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.service.ServiceRegistry;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author pogorelov
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    private static final ServiceRegistry serviceRegistry;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            Configuration configuration = new Configuration();
            configuration.configure("/db/config/svyatovydHibernate.cfg.xml");
            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static Page getPage(String pageName) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            if(pageName.equals("photos")){
                List photosData = session.createQuery("FROM PhotoMetaData").list(); 
                return createPageForImages(photosData);
            }
            if(pageName.equals("videos")){
                List videosData = session.createQuery("FROM VideoMetaData").list(); 
                return createPageForVideos(videosData);
            }
            List pages = session.createCriteria(Page.class).add(Restrictions.like("pageName", pageName)).list();
            tx.commit();
            return (Page)pages.get(0);
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return null;
    }
    
    public static Page createPageForImages(List<PhotoMetaData> lst) {
        if(lst.isEmpty()){
            return new Page(0, 0, "photos", "<NO PHOTO AVAILABLE>");
        }
        StringBuilder resStr = new StringBuilder("<div id=\"photo_container\">");
        for(PhotoMetaData data : lst){
            resStr.append("<div class=\"photo_album_item_container\">");
                resStr.append("<img src=\"");
                resStr.append(data.getPath());
                resStr.append("\">\n ");
            resStr.append("</div>");
        }
        resStr.append("</div>");
        return new Page(0, 0, "photos", resStr.toString());
    }
    
    public static Page createPageForVideos(List<VideoMetaData> lst) {
        if(lst.isEmpty()){
            return new Page(0, 0, "videos", "<NO VIDEO AVAILABLE>");
        }
        StringBuilder resStr = new StringBuilder("<div id=\"video_container\">");
        for(VideoMetaData data : lst){
            resStr.append("<div class=\"video_item_container\">");
                resStr.append("<iframe src=\"");
                resStr.append(data.getRef());
                resStr.append("\"></iframe>\n ");
            resStr.append("</div>");
        }
        resStr.append("</div>");
        return new Page(0, 0, "videos", resStr.toString());
    }
}


